import moment from 'moment';

export const createMonthArray = ([start, end], format = 'YYYY-MM') => {
  const s = moment(start)
    .startOf('month')
    .valueOf();
  const e = moment(end)
    .startOf('month')
    .valueOf();

  return Array(20)
    .fill(1)
    .reduce((pre, cur, index) => {
      const month = moment(s).add(index, 'month');
      if (month <= e) {
        pre.push(month.format(format));
      }
      return pre;
    }, []);
};

export const createWeekArray = ([startTime, endTime], value = {}) => {
  // const monthStart = moment(startTime).startOf('month');
  // const s1 = moment(startTime)
  //   .startOf('month')
  //   .day(1);
  // const e1 = moment(endTime).endOf('month');
  // const diff = e1.diff(s1, 'week') + 2;
  // return new Array(diff).fill(1).reduce((pre, cur, index) => {
  //   const ind = 7 * index;
  //   const date = s1.clone().add(ind, 'days');
  //   const dateEnd = date.clone().add(6, 'days');
  //   const month = date.clone().day(4);
  //   if (month >= s1 && month <= e1) {
  //     const item = {
  //       weekYear: date.weekYear(),
  //       week: date.week(),
  //       start:
  //         date < monthStart ? monthStart.format('MM.DD') : date.format('MM.DD'),
  //       end: dateEnd > e1 ? e1.format('MM.DD') : dateEnd.format('MM.DD'),
  //       month: month.format('YYYY-MM'),
  //     };
  //     pre.push(item);
  //   }
  //   return pre;
  // }, []);
  const monthStart = moment(startTime).startOf('month');
  const monthEnd = moment(endTime).endOf('month');

  const diff = monthEnd.diff(monthStart, 'day') + 1;
  const list = new Array(diff).fill(1).map((item, index) => {
    const date = monthStart.clone().add(index, 'days');
    return {
      weekYear: date.weekYear(),
      week: date.week(),
      month: date.format('YYYY-MM'),
      date: date.format('YYYY-MM-DD'),
      value: value[date.format('YYYY-MM-DD')] || '',
    };
  });
  // const month = list.reduce((pre, cur) => {
  //   if (pre[cur.month]) {
  //     pre[cur.month].push(cur);
  //   } else {
  //     pre[cur.month] = [cur];
  //   }
  //   return pre;
  // }, {});
  // Object.keys(month).map(item => {
  //   month[item] = month[item].reduce((pre, cur) => {
  //     if (pre[cur.week]) {
  //       pre[cur.week].push(cur);
  //     } else {
  //       pre[cur.week] = [cur];
  //     }
  //     return pre;
  //   }, {});
  // });
  // console.log('month: ', month);
  return list;
};
